package com.kenliang.javase.concurrent;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author KenLiang
 * @description:
 * @date Created in 10:32 AM 2019/12/17.
 */
public class CountDownLatchTest {

    public static void main(String[] args) throws InterruptedException {
        long now = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(2);
        Executor executor = Executors.newFixedThreadPool(2);
        executor.execute(()->{
            System.out.println("one--start");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("one--end");
            countDownLatch.countDown();

        });
        executor.execute(()->{
            System.out.println("two--start");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("two--end");
            countDownLatch.countDown();
        });
        countDownLatch.await();
        //调用这句话才会结束程序
        ((ExecutorService) executor).shutdownNow();
        System.out.println("over，回家 cost:"+(System.currentTimeMillis()-now));
    }
}
